﻿using System;
using System.ComponentModel;
using AuchanScan.Datas;

namespace AuchanScan.Models
{
    public class Achat : INotifyPropertyChanged
    {
        public bool UsePromo
        {
            get
            {
                if (Promotion == null)
                    return false;
                return Promotion.NbrNeededForPromo <= Quantity;
            }
        }

        public Product Product { get; set; }
        public int Quantity { get; set; }

        public double TotalPrice
        {
            get
            {
                if (Promotion == null || Promotion.NbrNeededForPromo > Quantity)
                    return Quantity*Product.Price;
                return (Quantity/Promotion.NbrNeededForPromo)*(Product.Price*Promotion.NbrNeededToPay) +
                       Product.Price*(Quantity%Promotion.NbrNeededForPromo);
            }
        }

        public double TVA
        {
            get { return Quantity*Product.TVA; }
        }

        public bool HasPromo
        {
            get { return Promotion != null; }
        }

        public Offre Promotion { get; set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void AddQuantity(int q)
        {
            Quantity += q;

            RaisePropertyChanged("UsePromo");
            RaisePropertyChanged("Quantity");
            RaisePropertyChanged("TotalPrice");
        }

        public void RaisePropertyChanged(String name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}